/*
 *  Copyright (C) 2010 MADSA.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author:  Jorge Pintado de Santiago
 *          
 */

package com.madsa.maps;

import java.io.IOException;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;

import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;

public class MapsRouteNavigator extends MapActivity {
	
    MyPositionOverlay positionOverlay;
	MapController mapController;
	private String destineDirection="";
	double src_lat; // the testing source
	double src_long;
	double dest_lat; // the testing destination
	double dest_long;
		
	@Override
	protected boolean isRouteDisplayed() {
	  return false;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Bundle extras  = getIntent().getExtras(); 
		if(extras != null){ 
			String destine = extras.getString("destino");
			destineDirection = destine.split(";")[1];
		}else{
			destineDirection = null;
		}
		
		LocationManager locationManager;
		String context = Context.LOCATION_SERVICE;
		locationManager = (LocationManager)getSystemService(context);
		
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setAltitudeRequired(false);
		criteria.setBearingRequired(false);
		criteria.setCostAllowed(true);
		criteria.setPowerRequirement(Criteria.POWER_LOW);
		
		String provider = locationManager.getBestProvider(criteria, true);
		
		Location location = locationManager.getLastKnownLocation(provider);
		updateWithNewLocation(location);
		
		Geocoder fwdGeocoder = new Geocoder(this);
		try{
			Address destineLocation = fwdGeocoder.getFromLocationName(destineDirection, 1).get(0);
			dest_lat = destineLocation.getLatitude();
			dest_long = destineLocation.getLongitude();
		}catch (IOException e){
		}
	
		String UrlMap = "http://maps.google.com/maps?f=d&source=s_d&saddr=(" + src_lat + "," + src_long + ")&daddr=(" + dest_lat + "," + dest_long + ")&hl=es&geocode=&mra=ls&ie=UTF8&t=h&z=15";
		Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(UrlMap));
		startActivity(myIntent);    				
		 
		/*
		 Podemos utilizar direcciones en vez de geolocalización. Es más exacto, y tenemos guardados así los cines
		 El único problema (menor) es sustituir los espacios por %20, y las tildes por sus correspondientes. 
		 */
		
	}
	
	private void updateWithNewLocation(Location location) {
		if (location != null) {		 
			src_lat = location.getLatitude();
			src_long = location.getLongitude();
		} 		
	}
}